class Solution {
public:
    int myAtoi(string str) {
        int flag = 0;
        int charte = 1;
        int count = 0;
        size_t num = 2;
        for (int j = 30; j >= 0; j--)
        {
            num *= 2;
        }
        while ((size_t)flag < str.size())
        {
            if (str[flag] == ' ' || str[flag] == '_')
            {

                ++flag;
                if (str[flag] == '-')
                {
                    charte = -1;
                    ++flag;
                }
            }
            else
            {
                break;
            }
        }
        while (str[flag] <= '9' && str[flag] >= '0')
        {

            int cut = str[flag] - '0';
            count = count * 10;
            count += cut;
            ++flag;
            if (flag == str.size())
            {
                break;
            }
        }
        count = charte * count;
        num = charte * num;
        if (count > num)
        {
            return num - 1;
        }
        else
            return charte * count;

    }
};